package com.gemme.gemme.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;
import com.gemme.gemme.util.DpUtil;

public class XinYuanDanView extends RelativeLayout {
    private Context context;
    float miniSize = 0.8f;
    public XinYuanDanView(Context context) {
        this(context,null);
    }

    public XinYuanDanView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public XinYuanDanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.xinyuandan_view,this);
//        DpUtil
       initData();
        System.out.println();
    }

    int screen_width;
    int screen_height;
    int sh_width;  //屏幕中轴线
    private void initData() {
        int left = getLeft();
        int right = getRight();
        try {
            Display defaultDisplay = ((Activity)context).getWindowManager().getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getSize(point);

            screen_width = point.x;
             sh_width = screen_width / 2;
            screen_height = point.y;
//            Log.i(TAG, "x = " + x + ",y = " + y);
            System.out.println("");
        }catch (Exception e){
            System.out.println("");
        }

        //view绘制完成时回调
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 已经完成绘制，干你要干的事情
                if (sh_width < 0)
                    return;

                control();
            }
        });

    }

    private void control() {
        float left = getLeft();
        float right = getRight();
        System.out.println();

        float view_half = (left + right) / 2;
        float abs = Math.abs(view_half - sh_width);
        if(abs >=  sh_width){
            scale(miniSize);
        }else{
            //  540
            float scale_size = 1 - Math.abs((((sh_width - view_half) / sh_width) * (1 - miniSize)));
            scale(scale_size);
        }
    }

    private void scale(float v) {
        /*AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 1.0f, v);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 1.0f, v);

//        animatorSetsuofang.setDuration(2000);
        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
        animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSetsuofang.start();*/
        setScaleY(v);
    }

    public void setChange() {
        control();
    }
}
